package com.example.lzbase;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gyf.barlibrary.ImmersionBar;
import com.kaopiz.kprogresshud.KProgressHUD;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.ThreadMode;

import androidx.annotation.LayoutRes;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;
import event.MessageEvent;

public abstract class BaseFragment extends BasePermissionsFragment {
    protected Context mContext;
    protected View rootView;

    @LayoutRes
    public abstract int getLayoutId();
    private KProgressHUD HUD;
    public void showLoading(boolean show){

        if(HUD==null){
            HUD = KProgressHUD.create(mContext)
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setCancellable(false)
                    .setDimAmount(0.1f);
        }
        if(show){
            HUD.show();
        }
        else {
            HUD.dismiss();
        }




    }
    @MainThread
    public abstract void createView();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {




        if (null != rootView) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (null != parent) {
                parent.removeView(rootView);
            }
        } else {
             rootView = inflater.inflate(getLayoutId(), container, false);

            ButterKnife.bind(this, rootView);
//      ImmersionBar.with(this);

            EventBus.getDefault().register(this);

            createView();
        }




        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initStatusBar();
    }
    public void initStatusBar(){

    }
    @org.greenrobot.eventbus.Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEventBus(MessageEvent event) {

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }
    @Override
    public void onDetach() {
        super.onDetach();

    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        mContext = null;

        EventBus.getDefault().unregister(this);


    }
}
